import {createRouter,createWebHashHistory} from 'vue-router'
import routes from './routes'
import storage from 'store'
import { TOKEN_KEY } from '@/utils/constants'

const router = createRouter({
	history: createWebHashHistory(),
	routes: routes
})
router.beforeEach(async (to, from) => {
  if(to.path !== '/login'){
	  let token =  undefined
	  if(storage.get('rememberMe')){
	  	token = storage.get(TOKEN_KEY)
	  }else{
	  	token = JSON.parse(sessionStorage.getItem(TOKEN_KEY))
	  }
	  if(token){
		  if(new Date().getTime() - token.addtime < 7 * 24 * 3600 * 1000){
			return true  
		  }else{ // 清除过期的token
			  storage.clearAll()
		  }
	  	  
	  }
	  return {path: '/login'}
  }
})
export default router
